﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Outlook = Microsoft.Office.Interop.Outlook;
using Office = Microsoft.Office.Core;

namespace Qero.ChartPoint
{
    public partial class ThisAddIn
    {
        private const string mapi = "MAPI";

        public static SQLiteClass dbControl = new SQLiteClass(System.IO.Path.Combine(BaseUnit.AppDirectory(BaseUnit.FolderType.DBFolder), Properties.Resources.DBFileName));

        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
            Outlook.MAPIFolder rootFolder = (Outlook.MAPIFolder)Application.GetNamespace(mapi).GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox).Parent;

            Outlook.MAPIFolder appRootFolder = ApplyFolder(rootFolder, Properties.Resources.AppName);
            if (appRootFolder == null) return;

            ApplyWebView(appRootFolder, typeof(AppRootControl));
            stdole.StdPicture appRootIcon = (stdole.StdPicture)PictureDispConverter.ToIPictureDisp(Properties.Resources.AppRootIcon);
            appRootFolder.SetCustomIcon(appRootIcon);
            
            Outlook.MAPIFolder appSetupFolder = ApplyFolder(appRootFolder, Properties.Resources.SetupName);
            if (appSetupFolder == null) return;

            ApplyWebView(appSetupFolder, typeof(AppSetControl));
        }

        private Outlook.MAPIFolder ApplyFolder(Outlook.MAPIFolder rootFolder, string applyName)
        {
            Outlook.MAPIFolder applyFolder = null;

            try { applyFolder = rootFolder.Folders[applyName]; }
            catch { applyFolder = rootFolder.Folders.Add(applyName, Outlook.OlDefaultFolders.olFolderInbox); }

            return applyFolder;
        }

        private void ApplyWebView(Outlook.MAPIFolder applyFolder, Type viewType)
        {
            applyFolder.WebViewURL = RegisterHomepage.RegisterType(viewType, applyFolder.Name);
            applyFolder.WebViewOn = true;
        }

        private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
        {
        }

        #region VSTO generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InternalStartup()
        {
            this.Startup += new System.EventHandler(ThisAddIn_Startup);
            this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
        }
        
        #endregion
    }
}
